/*
 * AnonymousStatement.c
 *
 *  Created on: Oct 2, 2008
 *      Author: V
 */

#include "AnonymousStatement.h"
#include <stdio.h>
#include "reservedWords.h"

pAnonymousStatement AnonymousStatement_new()
{
	pAnonymousStatement ret = (pAnonymousStatement) GC_malloc(sizeof(AnonymousStatement));
	ret->operator = String_new(NULL);
	ret->argumentList = NULL;
	ret->eval = &AnonymousStatement_eval;
	ret->toString = &AnonymousStatement_toString;
	//ret->clone = &AnonymousStatement_clone;
	return ret;
}

pString AnonymousStatement_eval(pAnonymousStatement statement)
{
	char* op = statement->operator->cString;
	pString ret = String_new(NULL);
	int debug = 1;
	if( op[0] == '#' )
	{
		if(!strcmp( op, "#first" ))
		{
			ret = RWFirst(statement);
			if (debug) printf("#first: %s\n", ret->cString);
		}
		else if(!strcmp( op, "#rest" ))
        {
			ret = RWRest(statement);
			if (debug) printf("#rest: %s\n", ret->cString);
        }
		else if(!strcmp( op, "#append" ))
        {
			ret = RWAppend(statement);
			if (debug) printf("#append: %s\n", ret->cString);
        }
        else if(!strcmp( op, "#isequal" ))
        {
        	ret = RWIsEqual(statement);
        	if (debug) printf("#isequal: %s\n", ret->cString);
        }
        else if(!strcmp( op, "#echo" ))
        {
			ret = RWEcho(statement);
        	if (debug) printf("#echo: %s\n", ret->cString);
        }
        else if(!strcmp( op, "#exit" ))
        {
			if (debug) printf("#exiting\n");
			RWExit();
        }
        else if(!strcmp( op, "#input" ))
        {
			ret = RWInput();
        	if (debug) printf("#input: %s\n", ret->cString);
        }
        else
		{
			printf("none of the above!\n");
		}

	}

	return ret;
}

pString AnonymousStatement_toString(pAnonymousStatement statement)
{
	pString ret = String_new(NULL);
	ret = ret->concat(ret, statement->operator);
	ret = ret->concat(ret, String_new("("));
	if (statement->argumentList == NULL)
	{
		return ret->concat(ret, String_new(")"));
	}
	int i = 0;
	struct ArgumentTag* currentArgument = statement->argumentList[i];
	while (currentArgument != NULL)
	{
		pString argString = (pString) Argument_toString(currentArgument);
		ret = ret->concat(ret, argString);
		currentArgument = statement->argumentList[++i];
		if (currentArgument != NULL) ret = ret->concat(ret, String_new(", "));
	}

	return ret->concat(ret, String_new(")"));
}

/*pAnonymousStatement AnonymousStatement_clone(pAnonymousStatement statement)
{
	pAnonymousStatement ret = AnonymousStatement_new();
	ret->operator = statement->operator;
	ret->argumentList = Argument_cloneList(statement->argumentList);
	return ret;
}*/

AnonymousStatementList AnonymousStatement_cloneList(AnonymousStatementList list)
{
	return NULL;
}
